﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Wjtour.Common;
using Hjp.FrameWork.Common;
using Wjtour.BLL;
using System.Linq;
using TongCheng.API.OpenAPI;
using TongCheng.API.OpenAPI.Domain;
using TongCheng.API.OpenAPI.Request;
using TongCheng.API.OpenAPI.Facade;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace Wjtour.Web.Front
{
    public partial class ticketlist : BasePage
    {
        #region 固定分页参数
        protected int page = 1;
        protected int pagesize = 5;
        #endregion

        slides_bll _slides_bll = new slides_bll();
        protected List<Wjtour.Domain.line> recommendLines = new List<Domain.line>();
        Wjtour.BLL.line_bll _linebll = new line_bll();
        protected List<Wjtour.Domain.article> trafficGuides = new List<Domain.article>();//旅游攻略
        Wjtour.BLL.article_bll _articleBll = new article_bll();
        protected List<Wjtour.Domain.slides> _slides = new List<Domain.slides>();
        protected TongCheng.API.OpenAPI.Domain.SceneryList list = new TongCheng.API.OpenAPI.Domain.SceneryList();
        protected TongCheng.API.OpenAPI.Domain.SceneryList list2 = new TongCheng.API.OpenAPI.Domain.SceneryList();
        protected TongCheng.API.OpenAPI.Domain.SceneryList sceneryTicklist = new TongCheng.API.OpenAPI.Domain.SceneryList();
        protected List<int> scenaryIds = new List<int>();
        protected Dictionary<int, TongCheng.API.OpenAPI.Domain.Scenery> sceneryTickDic = new Dictionary<int, Scenery>();

        protected TextBox txtComeDate;
        protected TextBox txtLeaveDate;
        protected TextBox txtLocation;
        protected TextBox txtHotelName;
        protected TextBox txtSceneryName;
        protected Button hotelSearch, scenerySearch;

        #region 参数
        protected string comeDate = DateTime.Now.ToString("yyyy-MM-dd");
        protected string leaveDate = DateTime.Now.AddDays(1).ToString("yyyy-MM-dd");
        protected string s = "";
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            page = Utils.StrToInt(Request["page"], 1);
            s = Request["s"];
            hotelSearch.Click += hotelSearch_Click;
            scenerySearch.Click += scenerySearch_Click;
            if (!string.IsNullOrWhiteSpace(s))
            {
                txtSceneryName.Text = s;
            }
            if (!IsPostBack)
            {
                getScenery();
            }
            bindList();
        }

        void bindList()
        {
            _slides = _slides_bll.GetList(4);
            recommendLines = _linebll.GetAll<Wjtour.Domain.line>("select top 6 * from line where isrecommend=1").ToList();
            trafficGuides = _articleBll.GetAuditTopListWithFirstLogo("6", 7, 2);
        }

        void scenerySearch_Click(object sender, EventArgs e)
        {
            getScenery();
        }

        void hotelSearch_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtComeDate.Text))
            {
                Utils.Alert(this.Page, "请选择入住日期！");
                return;
            }
            if (string.IsNullOrEmpty(txtLeaveDate.Text))
            {
                Utils.Alert(this.Page, "请选择离店日期！");
                return;
            }
            DateTime dt1 = DateTime.Now;
            DateTime dt2 = DateTime.Now.AddDays(1);
            DateTime.TryParse(txtComeDate.Text, out dt1);
            DateTime.TryParse(txtLeaveDate.Text, out dt1);
            if (dt1 > dt2)
            {
                Utils.Alert(this.Page, "入住日期不能晚于离店日期！");
                return;
            }
            comeDate = txtComeDate.Text;
            leaveDate = txtLeaveDate.Text;

            string query = string.Format("cd={0}&ld={1}&loc={2}&n={3}", comeDate, leaveDate, txtLocation.Text, txtHotelName.Text);
            Response.Redirect("hotellist.aspx?" + query);
        }

        void getScenery()
        {
            GetSceneryListRequest request = new GetSceneryListRequest();
            request.Body.ClientIp = "172.0.0.1";
            request.Body.ProvinceId = 16;
            request.Body.CityId = 226;
            request.Body.Page = page;
            request.Body.Cs = "1";
            request.Body.SortType = 0;
            request.Body.GradeId = "1,2,3,4,5";
            request.Body.PageSize = pagesize;
            if (!string.IsNullOrEmpty(txtSceneryName.Text))
            {
                request.Body.Keyword = txtSceneryName.Text;
                request.Body.SearchFields = "sceneryName";
            }

            //XmlSerializer serializer2 = new XmlSerializer(typeof(GetSceneryListRequest));
            //FileStream fs2 = new FileStream("c:\\31.xml", FileMode.Create);
            //serializer2.Serialize(fs2, request);
            //fs2.Close();
            //return;

            #region 发送请求(景点信息)，并解析返回数据
            var response = SceneryFacade.GetSceneryList(request);
            list = response.Body.sceneryList;
            string query = string.Format("s={0}", txtSceneryName.Text);
            PagnationHtml = GetPageHtml("ticketlist.aspx?" + query, page, list.TotalPage);
            #endregion

            scenaryIds = list.Items.Select(s => s.sceneryId).ToList();

            #region 发送请求(查询景点门票信息)，并解析返回数据
            GetSceneryPriceRequest re = new GetSceneryPriceRequest();
            re.Body.ShowDetail = 1;
            re.Body.SceneryIds = string.Join(",", scenaryIds.ToArray());
            re.Body.UseCache = 0;


            var response2 = SceneryFacade.GetSceneryPrice(re);
            list2 = response2.Body.sceneryList;
            foreach (var item in list2.Items)
            {
                sceneryTickDic.Add(item.sceneryId, item);
            }
            #endregion
        }
    }
}
